﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Data.OleDb;
using System.IO;
using System.Data;
using SDD;
using NPOI.HSSF.UserModel;

namespace SDD
{
    class FundRate
    {
        public string url1 = "http://yamstock.megatime.com.tw/asp/realquote/quote.asp?menu=tse&marketindex=2&kindindex=0&m=HTML";

        private string FullFileName = Program.FileDir + "類股資金比.xls";

        public string dataDay = DateTime.Today.Year.ToString() + DateTime.Today.Month.ToString("00") + DateTime.Today.Day.ToString("00");
        public string msg = "";


        public void AggregateData()
        {
            try
            {
                analysisHTML(url1);
            }
            catch 
            {
                Console.WriteLine("類股資金比 資料擷取失敗!!");
            }
        }
        /// <summary>
        /// 解析HTML new
        /// </summary>
        /// <param name="url"></param>
        /// <param name="ds"></param>
        public void analysisHTML(string url)
        {
            string context = new cHttp().dopost(url);
            WebBrowser wbr = new WebBrowser();
            wbr.Navigate("");

            wbr.ScriptErrorsSuppressed = true;
            wbr.Document.Write("<script type='text/javascript'>window.onerror = function(){};</script>" + context);

            HtmlElementCollection tb = wbr.Document.GetElementsByTagName("TABLE");
            HtmlElementCollection tr = tb[13].GetElementsByTagName("TR");
            string[] dataday = tb[12].InnerText.Substring(tb[12].InnerText.IndexOf("：") + 1).Split('/');

            /////////////////////
            //建立工作表
            NPOIComFunction xls = new NPOIComFunction(FullFileName);
            HSSFSheet sheet = null;
            if (xls.Workbook.GetSheetIndex("Sheet1") > -1)
                sheet = xls.Workbook.GetSheet("Sheet1");
            else
            {
                //新的資料表 
                //寫入類股名稱
                sheet = xls.Workbook.CreateSheet("Sheet1");
                sheet.CreateRow(0).CreateCell(0).SetCellValue("類股");
                int i = 1;
                foreach (HtmlElement trElement in tr)
                {
                    HtmlElementCollection td = trElement.GetElementsByTagName("TD");
                    if (td[0].InnerText.Substring(0, 1).ToUpper().Equals("M"))
                    {
                        sheet.CreateRow(i).CreateCell(0).SetCellValue(td[1].InnerText);
                        i++;
                    }
                } // end foreach

            } // end if

            //////////////////////////
            //寫入類股量佔比
            //找出從哪裡開始寫
            int cellid = 1;
            while (sheet.GetRow(0).GetCell(cellid) != null)
            {
                cellid++;
            }

            string ToDayDataTime = string.Format("{0}/{1}/{2}", dataday[0], dataday[1], dataday[2]);
            try
            {
                if (sheet.GetRow(0).GetCell(cellid - 1).StringCellValue == ToDayDataTime)
                {
                    Console.WriteLine("類股資金比 " + dataDay + " 資料已經存在!!!");
                    return;
                }
            }
            catch
            {

            }
            // 日期
            HSSFCell cell = sheet.GetRow(0).CreateCell(cellid);
            cell.SetCellValue(ToDayDataTime);

            // 類股資金比
            int j = 1;
            foreach (HtmlElement trElement in tr)
            {
                HtmlElementCollection td = trElement.GetElementsByTagName("TD");
                if (td[0].InnerText.Substring(0, 1).ToUpper().Equals("M"))
                {
                    sheet.CreateRow(j).CreateCell(cellid).SetCellValue(td[8].InnerText);
                    j++;
                }
            } // end foreach

            // 寫入實體檔案
            xls.WriteToFile();
        }


    }
}
